package util

import (
	"crypto/md5"
	"encoding/hex"
	"strings"
)

func ReturnData(code int, msg string, data interface{}) map[string]interface{} {
	response := make(map[string]interface{}, 3)
	response["data"] = data
	response["msg"] = msg
	response["code"] = code
	return response
}

func ReturnSuccess(data interface{}) map[string]interface{} {
	response := make(map[string]interface{}, 3)
	response["data"] = data
	response["msg"] = "success"
	response["code"] = 0
	return response
}

func ReturnError(errCode int, errMsg string) map[string]interface{} {
	response := make(map[string]interface{}, 3)
	response["data"] = nil
	response["msg"] = errMsg
	response["code"] = errCode
	return response
}

func MD5(in string) (string, error) {
	hash := md5.Sum([]byte(in))
	return hex.EncodeToString(hash[:]), nil
}

func ShowValueWithDefault(data interface{}, defaultVal interface{}) interface{} {
	if data == nil || data == "" {
		return defaultVal
	}
	return data
}

// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
	if s == "" {
		return ""
	}
	return strings.ToUpper(s[:1]) + s[1:]
}

// FirstLower 字符串首字母小写
func FirstLower(s string) string {
	if s == "" {
		return ""
	}
	return strings.ToLower(s[:1]) + s[1:]
}
